'''
Created on 04/05/2013

@authors: jean
'''
from . import ensureParams, jsonifiedview
from flask import Flask, session, redirect, url_for, escape, request, Response
from pm import app
from pm.database import db
from pm.models.TiposItemAtributo import TiposItemAtributo

@app.route('/tipoitematributo/asinaratributo', methods=['GET', 'POST']) 
@jsonifiedview

def asignarAtributoaTipodeItem():
    '''
        Asigna un atributo a un tipo de item 
    '''
    params = {'GET':request.args, 'POST':request.form}[request.method]
    response = {}
    
    if not ensureParams(params,['id_atributo','id_tipo_item','activo', 'valor']): 
        response ={"success" : False , "msg" : "Parametros Insuficientes"} 
        return response
    
    try:
        tipoItemAtributo = TiposItemAtributo(params['id_tipo_item'],params['id_atributo'], params['valor'],params['activo'])
        db.session.add(tipoItemAtributo)
        db.session.commit()
        response ={"success" : True , "msg" : "Creacion exitosa"} 
        return response
    
    except Exception, e:
        db.session.rollback()
        print e
        response ={"success" : False , "msg" : "Error en la base de datos"}
        return response 
       
@app.route('/tipoitematributo/activardesactivar', methods=['GET', 'POST']) 
@jsonifiedview
def activarDesactivarAtributoaTipodeItem():
    '''
        activa o inactiva un atributo a un tipo de item 
    '''
    params = {'GET':request.args, 'POST':request.form}[request.method]
    response = {}
    
    if not ensureParams(params,['activo','id_atributo','id_tipo_item']): 
        response ={"success" : False , "msg" : "Parametros Insuficientes"} 
        return response
    
    try:
        tipoItemAtributo = TiposItemAtributo.query.filter(TiposItemAtributo.id_atributo == params ['id_atributo']).filter(TiposItemAtributo.id_tipo_item == params ['id_tipo_item']).update({'activo':params['activo']})
        db.session.commit()
        response ={"success" : True , "msg" : "Actualizacion exitosa"} 
        return response
    
    except Exception, e:
        db.session.rollback()
        response ={"success" : False , "msg" : "Error en la base de datos"}
        return response 
    
@app.route('/tipoitematributo/modificar',methods=['GET','POST'])
@jsonifiedview
def modificarTipoItemAtributo():
    '''
       modifica el valor entre un tipo de item y un atributo
    '''
    params = {'GET':request.args, 'POST':request.form}[request.method]
    response = {}
    
    if not ensureParams(params,['valor','id_atributo','id_tipo_item']): 
        response ={"success" : False , "msg" : "Parametros Insuficientes!!"} 
        return response

    try:
        tipoItemAtributo = TiposItemAtributo.query.filter(TiposItemAtributo.id_atributo == params ['id_atributo']).filter(TiposItemAtributo.id_tipo_item == params ['id_tipo_item']).update({'valor':params['valor']})
        db.session.commit()
        response ={"success" : True , "msg" : "Actualizacion exitosa!!"} 
        return response
    
    except Exception, e:
        db.session.rollback()
        response ={"success" : False , "msg" : "Error en la base de datos!!"}
        return response 

@app.route('/tipoitematributo/obtenervalor',methods=['GET','POST'])
@jsonifiedview
def obtenerValorRelacion():
    '''
       modifica el valor entre un tipo de item y un atributo
       @param id_tipo_item: el tipo de item
       @param id_atributo: el id del atributo del cual se desea otener la relacion
    '''
    params = {'GET':request.args, 'POST':request.form}[request.method]
    
    if not ensureParams(params,['id_tipo_item','id_atributo']): 
        response ={"success" : False , "msg" : "Parametros Insuficientes!!", "valor" : None} 
        return response
    try:
        tipoItemAtributo = TiposItemAtributo.query.filter(TiposItemAtributo.id_tipo_item == params['id_tipo_item']).filter(TiposItemAtributo.id_atributo == params ['id_atributo']).one()    
        resp = tipoItemAtributo.valor
        response ={"success" : True , "msg" : "Valor encontrado exitosamente", "valor" : resp}
        return response
    
    except Exception, e:
        response ={"success" : True , "msg" : "Error en la base de datos", "valor" : None}
    